<?php
/**
 * Created by PhpStorm.
 * User: dancheng
 * Date: 2018/1/18
 * Time: 12:22
 */
class User extends CI_Controller{
    //数据传输
    public $token_data;
    /**
     * User constructor.
     * 构造函数，导入url辅助类
     */
    public function __construct(){
            parent::__construct();
            $this->load->model('user_model');
            $userToken = new Usertoken_helper();
            $this->token_data['token'] = $userToken->check_token($this);
            $this->token_data['user_token'] = $userToken->check_userToken($this);
    }

    //主页
    public function index(){$this->load->view('user/allUsers');}
    //添加用户
    public function addUser(){$this->load->view('user/addUser');}
    //修改用户
    public function updateUser(){$this->load->view('user/updateUser');}

    /**
     * userList接口
     */
    public function imp_index(){
        if($this->token_data['user_token']['flag']){
            //如果访问合法便进行数据操纵
            if($this->token_data['token']['tag']){
                $data = $this->user_model->get_All();
                $this->token_data['data'] = $data;
            }
            $json = json_encode($this->token_data);
            echo $json;
        } else {
            echo '0';
        }
    }

    /**
     * 添加接口
     */
    public function imp_add(){
        if($this->token_data['user_token']['flag']){
            if($this->token_data['token']['tag']){
                $data = $_POST['data'];
                $arr = json_decode($data, true);
                $arr['password'] = md5($arr['password']);
                $arr['create_time']=date("Y-m-d H:i:s");
                $this->user_model->add_user($arr);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        } else {
            echo '0';
        }
    }

    /**
     * 删除接口
     */
    public function imp_del(){
        if($this->token_data['user_token']['flag']){
            if($this->token_data['token']['tag']){
                $user_id = $_POST['user_id'];
                $this->user_model->delete_user($user_id);
            }
            $json = json_encode($this->token_data);
            echo $json;
        } else {
            echo '0';
        }
    }

    /**
     * 修改接口
     */
    public function imp_update(){
        if($this->token_data['user_token']['flag']){
            if($this->token_data['token']['tag']){
                $data = $_POST['data'];
                $arr = json_decode($data, true);
                $this->user_model->update_user($arr);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        } else {
            echo '0';
        }
    }

    /**
     * 回显接口
     */
    public function imp_edit(){
        if($this->token_data['user_token']['flag']){
            if($this->token_data['token']['tag']){
                $user_id = $_POST['user_id'];
                $this->token_data['user'] = $this->user_model->getUserById($user_id);
                $this->token_data['success'] = 1;
            }
            $json = json_encode($this->token_data);
            echo $json;
        } else {
            echo '0';
        }
    }

    /**
     * 用户名模糊查询接口.
     */
    public function imp_find(){
        if($this->token_data['user_token']['flag']){
            //如果访问合法便进行数据操纵
            if($this->token_data['token']['tag']){
                $data=$_GET['data'];
                $data = $this->user_model->findlike($data);
                $this->token_data['data'] = $data;
            }
            $json = json_encode($this->token_data);
            echo $json;
        } else {
            echo '0';
        }
    }

    /**
     * 获取用户菜单
     */
    public function menu(){
        if($this->token_data['user_token']['flag']){
            //如果访问合法便进行数据操纵
            if($this->token_data['token']['tag']){
                $data=$_GET['user_token'];
                $data = json_decode($data, true);
                $id=$data['user_id'];
                $data = $this->user_model->get_menu($id);
                $this->tree($data);
                $this->token_data['data'] = $data;
            }
            $json = json_encode($this->token_data);
            echo $json;
        } else {
            echo '0';
        }
    }

    /**
     * @param $data
     * 递归方法，将用户的所有菜单组建成有等级的菜单
     */
     public function tree(&$data){
            $num=0;//变化？
            $need=0;//是否需要改变
             foreach($data as $v=>&$c){
                 $status=0;//不能改变？
                 $c['spread']=false;
                 if($c['parentid']!=1){//如果不是最上级节点
                     $need=1;
                     foreach($data as $k){
                         if($k['parentid']==$c['id']){
                             $status=1;
                         }
                     }
                     if($status==0){//如果不是其他的父节点
                         foreach($data as &$b)//添加到父节点的children中
                         {
                             if($b['id']==$c['parentid']){
                                 $b['children'][]=$c;
                                 array_splice($data,$v,1);//删除
                                 $num=1;
                                 break;
                             }
                         }
                     }
                 }
             }
            if($num==0&&$need==0){
                return;
            }else{
                $this->tree($data);
            }
     }

}